//
//  ToolControl.m
//  db
//
//  Created by 张育铭 on 2019/7/31.
//  Copyright © 2019 赢辉网络科技有限公司. All rights reserved.
//

#import "ToolControl.h"

@implementation ToolControl

+ (UIView *)creatNaViewWithFrame:(CGRect)frame {
    UIView *view = [[UIView alloc]initWithFrame:frame];
    view.backgroundColor = RGBCOLOR(243, 202, 108);
    return view;
}



+ (UIButton *)creatRoundButtonWithFrame:(CGRect)frame Title:(NSString *)title Font:(CGFloat)font target:(id)target selector:(SEL)sel {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:font];
    [btn setBackgroundColor:[UIColor lightGrayColor]];
    btn.layer.cornerRadius  = 4;
    btn.layer.masksToBounds = YES;
    [btn addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

+ (UIButton *)creatButtonWithFrame:(CGRect)frame Title:(NSString *)title Font:(CGFloat)font image:(UIImage *)image target:(id)target selector:(SEL)sel {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setImage:image forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal]; 
    [btn addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
    btn.titleLabel.font = [UIFont systemFontOfSize:font];
    return btn;
}

+ (UIButton *)creatButtonRightWithFrame:(CGRect)frame Title:(NSString *)title Font:(CGFloat)font image:(UIImage *)image target:(id)target selector:(SEL)sel {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setImage:image forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, - btn.imageView.image.size.width, 0, btn.imageView.image.size.width)];
    [btn setImageEdgeInsets:UIEdgeInsetsMake(0, btn.titleLabel.bounds.size.width, 0, -btn.titleLabel.bounds.size.width)];
    [btn addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
    btn.titleLabel.font = [UIFont systemFontOfSize:font];
    return btn;
}

+ (UIButton *)creatButtonTopWithFrame:(CGRect)frame Title:(NSString *)title Font:(CGFloat)font image:(UIImage *)image target:(id)target selector:(SEL)sel{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setImage:image forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
    btn.titleLabel.font = [UIFont systemFontOfSize:font];
    [self initButton:btn];
    return btn;
}

+ (void)initButton:(UIButton*)btn{
    //使图片和文字水平居中显示
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  //文字距离上边框的距离增加imageView的高度，距离左边框减少imageView的宽度，距离下边框和右边框距离不变
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(btn.imageView.frame.size.height + 20 ,-btn.imageView.frame.size.width, 0.0,0.0)];
    //图片距离右边框距离减少图片的宽度，其它不边
    [btn setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0,10, -btn.titleLabel.bounds.size.width)];
}

+ (UILabel *)creatTitleLabelWithFrame:(CGRect)frame theText:(NSString*)text theTextFont:(CGFloat)font {
    UILabel *headText = [[UILabel alloc] initWithFrame:frame];
    headText.text = text;
    headText.font = [UIFont systemFontOfSize:font];
    headText.textColor = [UIColor blackColor];
    headText.backgroundColor = [UIColor clearColor];
    return headText;
}

+ (UILabel *)creaImagetTitleLabelWithFrame:(CGRect)frame theImageStr:(NSString*)image theImageWidth:(CGFloat)width theText:(NSString*)text {
    UIImageView *leftImage = [[UIImageView alloc]initWithFrame:CGRectMake(-width, (frame.size.height-width)/2, width, width)];
    leftImage.image = [UIImage imageNamed:image];
    leftImage.backgroundColor = [UIColor clearColor];
    
    UILabel *headText = [[UILabel alloc] initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height)];
    headText.text = text;
    headText.font = [UIFont systemFontOfSize:16];
    headText.textColor = [UIColor blackColor];
    headText.backgroundColor = [UIColor clearColor];
    [headText addSubview:leftImage];
    return headText;
}

+ (UITextField *)creatTextFieldWithFrame:(CGRect)frame thePlaceText:(NSString*)text theTextFont:(CGFloat)font {
    UITextField *textField = [[UITextField alloc] initWithFrame:frame];
    textField.font            = [UIFont systemFontOfSize:font];
    textField.placeholder     = text;
    textField.returnKeyType   = UIReturnKeyDone;
    return textField;
}




+ (NSString *)compareCurrentTime:(NSString *)str {
    
    //把字符串转为NSdate
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
    NSDate *timeDate = [dateFormatter dateFromString:str];
    
    //得到与当前时间差
    NSTimeInterval  timeInterval = [timeDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    //标准时间和北京时间差8个小时
    timeInterval = timeInterval - 8*60*60;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%ld分钟前",temp];
    }
    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小时前",temp];
    }
    else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%ld月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    return  result;
}
+ (NSString*)getTimeFromTimestamp:(NSString *)timeStr {
    //将对象类型的时间转换为NSDate类型
    double time = [timeStr doubleValue];
    NSDate *myDate = [NSDate dateWithTimeIntervalSince1970:time];
    //设置时间格式
    NSDateFormatter * formatter=[[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss.SSS"];
    //将时间转换为字符串
    NSString *timeStrS = [formatter stringFromDate:myDate];
    return timeStrS;
}





@end
